package com.sinopec.basemodule.dialog;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sinopec.basemodule.R;
import com.sinopec.basemodule.adapter.CommonAdapter;
import com.sinopec.basemodule.adapter.CustomViewHolder;
import com.sinopec.basemodule.model.StockOutProductTypes;
import com.sinopec.basemodule.utils.kotlin.StringEKt;
import com.sinopec.basemodule.widget.popupwindow.BasePopup;

import java.util.List;

/**
 * @author lwj
 * @ClassName: SkuStatisticsPopupWindow
 * @Description: 出库-sku数量统计弹窗
 */
public class SkuStatisticsPopupWindow extends BasePopup<SkuStatisticsPopupWindow> {
    private Context mContext;
    private OnSelectListener listener;
    private RecyclerView rv_list;
    private List<StockOutProductTypes> models;
    private CommonAdapter<StockOutProductTypes> adapter;
    //关闭
    private TextView tv_close;

    public static SkuStatisticsPopupWindow create(Context context, List<StockOutProductTypes> models, OnSelectListener listener) {
        return new SkuStatisticsPopupWindow(context, models, listener);
    }

    public SkuStatisticsPopupWindow(Context context, List<StockOutProductTypes> models, OnSelectListener listener) {
        this.listener = listener;
        this.mContext = context;
        this.models = models;
        setContext(context);
    }


    @Override
    protected void initAttributes() {
        setContentView(R.layout.choose_sku_statistics_popupwindow);
        DisplayMetrics d = mContext.getResources().getDisplayMetrics();
        //设置弹窗宽高比例IntEKt.dp2px(370)
        this.setWidth((int) (d.widthPixels * 0.50));
        this.setHeight((int) (d.heightPixels * 0.80));

        setFocusAndOutsideEnable(true)//是否PopupWindow 以外触摸dissmiss
                .setBackgroundDimEnable(true)
                .setNeedReMeasureWH(true)
                .setDimValue(0.5f);

//        setContentView(R.layout.choose_warehouse_popupwindow, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        setFocusAndOutsideEnable(true)
//                .setBackgroundDimEnable(true)
//                .setNeedReMeasureWH(true)
//                .setDimValue(0.5f);
    }

    @Override
    protected void initViews(View view, SkuStatisticsPopupWindow basePopup) {
        rv_list = view.findViewById(R.id.rv_list);
        adapter = new CommonAdapter<StockOutProductTypes>(mContext, R.layout.sku_statistics_item_popuwindow, models) {
            @Override
            public void convert(CustomViewHolder holder, StockOutProductTypes model, int position) {
                View itemView = holder.itemView;
                //业务类型
                TextView tv_productTypeName = holder.getView(R.id.tv_productTypeName);
                tv_productTypeName.setText(model.getProductTypeName());
                //计划出库
                TextView tv_planCount = holder.getView(R.id.tv_planCount);
                tv_planCount.setText(StringEKt.parseInt(model.getPlanCount()) + "");
                //当前已读编码
                TextView tv_actualStockOutCount = holder.getView(R.id.tv_actualStockOutCount);
                tv_actualStockOutCount.setText(StringEKt.parseInt(model.getActualStockOutCount()) + "");

                View v_line = holder.getView(R.id.v_line);
                if (models.size() == Integer.parseInt((position + 1) + "")) {
                    v_line.setVisibility(View.GONE);
                } else {
                    v_line.setVisibility(View.VISIBLE);
                }
            }
        };
        rv_list.setAdapter(adapter);

        //关闭弹窗
        tv_close = view.findViewById(R.id.tv_close);
        tv_close.setOnClickListener((View view1) -> {
            listener.onClose();
        });
    }

    public interface OnSelectListener {
        /**
         * 关闭
         */
        void onClose();
    }
}
